//
//  DSBaseViewController.m
//  IntelligentLock
//
//  Created by yj_zhang on 2019/5/24.
//  Copyright © 2019 yj_zhang. All rights reserved.
//

#import "DSBaseViewController.h"
#import "CommonHeader.h"

@interface DSBaseViewController ()

@end

@implementation DSBaseViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.navigationItem setHidesBackButton:YES];
//    self.backBtn.hidden = self.navigationController.viewControllers.count <= 1;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    self.backBtn.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureDefaultNavBar];
}

//MARK:设置导航栏颜色
- (void)configureDefaultNavBar{
    [self setNavigationBarStyle:BarTint_Clear];
    self.backBtn = [self leftBackBtn];
    [self.navigationController.navigationBar addSubview:self.backBtn];
    [self.navigationController.navigationBar addSubview:self.backTitleLB];
//    self.backBtn.hidden = YES;
}
- (void)setNavigationBarStyle:(NavigationBarTintStyle)barStyle{
    switch (barStyle) {
        case BarTint_Default:{
            UIImage * bgImg = [UIImage jk_imageWithColor:[UIColor whiteColor]];
            UIImage * shadowImg = [UIImage jk_imageWithColor:KLineColor];
            self.navigationController.navigationBar.translucent = NO;
            [self.navigationController.navigationBar setBackgroundImage:bgImg forBarMetrics:UIBarMetricsDefault];
            [self.navigationController.navigationBar setShadowImage:shadowImg];
        }
            break;
        case BarTint_Clear:{
            UIImage * bgImg = [UIImage jk_imageWithColor:[UIColor clearColor]];
            UIImage * shadowImg = [UIImage jk_imageWithColor:[UIColor clearColor]];
            self.navigationController.navigationBar.translucent = YES;
            [self.navigationController.navigationBar setBackgroundImage:bgImg forBarMetrics:UIBarMetricsDefault];
            [self.navigationController.navigationBar setShadowImage:shadowImg];
        }
            break;
        default:
            break;
    }
}
//Title
-(void)setNavigationTitle:(NSString*)navTitle{
    UIFont *font = KIMFontMedium(17.5);
    NSDictionary *textdic = @{NSFontAttributeName:font,
                              NSForegroundColorAttributeName: [UIColor jk_colorWithHexString:@"#333333"]};
    self.navigationController.navigationBar.titleTextAttributes =textdic;
    self.navigationItem.title = navTitle;
}

//MARK:设置 返回&右侧按钮
//返回键
- (UIButton *)leftBackBtn{
    CGRect buttonFrame = kNavBtnRect;
    buttonFrame.origin = CGPointMake(2, 0);
    UIButton *backBtn = [[UIButton alloc] initWithFrame:buttonFrame];
    backBtn.backgroundColor = [UIColor clearColor];
    backBtn.tag = 222;
    [backBtn setImage:[UIImage imageNamed:@"base_back"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"base_back"] forState:UIControlStateHighlighted];
    backBtn.contentMode = UIViewContentModeCenter;
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return backBtn;
}

- (UILabel *)backTitleLB{
    if(!_backTitleLB){
        _backTitleLB = [UILabel new];
        _backTitleLB.textColor = [UIColor jk_colorWithHexString:@"#333333"];
        _backTitleLB.font = KIMFontRegularSize(14);
        _backTitleLB.frame = CGRectMake(self.backBtn.jk_right - 3, self.backBtn.jk_top,IMSCREEN_WIDTH-adaptWidth750(40), self.backBtn.jk_height);
        _backTitleLB.numberOfLines=1;
        //RAC(_backTitleLB,hidden) = RACObserve(self.backBtn, hidden);
    }
    return _backTitleLB;
}
- (void)setBackBtnTitle:(NSString *)backTitle{
    [self.backBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.backBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
    [self.backBtn setTitle:backTitle forState:UIControlStateNormal];
    self.backBtn.tag = 222;
    [self.backBtn setTitleColor:[UIColor jk_colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    self.backBtn.titleLabel.font = KIMFontRegularSize(16);
    self.backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.backBtn.jk_left = KDefault_LeftMargin;
}
//设置右侧btn名称
- (void)setRightBtnTitle:(NSString *)rightBtnTitle{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    self.rightBtn = btn;
    btn.titleLabel.font = KIMFontRegularSize(16);
    CGSize size = CGSizeMake(IMSCREEN_WIDTH,2000); // 设置一个行高上限
    NSDictionary *attribute = @{NSFontAttributeName: btn.titleLabel.font,NSForegroundColorAttributeName:[UIColor jk_colorWithHexString:@"#666666"]};
    CGSize btnsize = [rightBtnTitle boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    
    float titleWidth = btnsize.width;
    btn.frame = CGRectMake(0, 0, titleWidth, 44);
    [btn setTitle:rightBtnTitle forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor jk_colorWithHexString:@"333333"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
}
//设置右侧btn图标
- (void)setRightBtnImg:(NSString *)imgName{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn = btn;
    [btn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(0, 0, 44, 44);
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    
    UIBarButtonItem *backBtnI = [[UIBarButtonItem alloc] initWithCustomView:btn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = adaptWidth750(19*2);
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backBtnI];
    
}


//MARK:Actions
//返回
- (void)backAction:(UIButton *)btn{
    if(self.navigationController.viewControllers.count<=1){
        [self dismissViewControllerAnimated:YES completion:nil];
    }else if (self.navigationController.viewControllers[0] != self)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
//右侧
- (void)rightAction:(UIButton *)rightBtn{
    NSLog(@"右侧按钮");
}


//MARK:加载页 & 断网页 & 空白页
//设置加载动画
- (void)setLoadingAnimationFrame:(CGRect)frame imgArr:(NSMutableArray*)arr{
    if (self.loadingView.window != nil) {
        [self.loadingView removeFromSuperview];
    }
    self.loadingView = [[StatisticLoadingView alloc]initWithFrame:frame imgArr:arr];
    [self.view addSubview:self.loadingView];
}
//设置无网页
- (void)setUnNetPageFrame:(CGRect)frame imgArr:(NSMutableArray*)arr reloadSel:(SEL)sel target:(UIViewController *)target{
    if (self.noNetView.window != nil) {
        [self.noNetView removeFromSuperview];
    }
    self.noNetView = [[StatisticNetUnReachView alloc]initWithFrame:frame];
    [self.view addSubview:self.noNetView];
    self.noNetView.reloadBlock = ^(){//重新请求数据
        [((DSBaseViewController *)target) setLoadingAnimationFrame:frame imgArr:arr];
        IMP imp = [target methodForSelector:sel];
        void (*func)(id, SEL) = (void *)imp;
        func(target, sel);
    };
}

//有网时隐藏加载页和断网页
-(void)hiddenNetAndLoadingView{
    [self.loadingView.animatImgView stopAnimating];
    self.loadingView.hidden=YES;
    self.noNetView.hidden=YES;
}



- (void)dealloc{
    [self.backBtn removeFromSuperview];
}


@end
